﻿using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Heyo.Class;
using Heyo.Class.Helper;
using HorizontalAlignment = System.Windows.HorizontalAlignment;

namespace Heyo.Controls.Animations
{
    public class WaveToggle : AnimationLayer
    {
        [Category("Animation")]

        public Brush ActiveBrush
        {
            get => (Brush)GetValue(ActiveBrushProperty);
            set => SetValue(ActiveBrushProperty, value);
        }

        public static readonly DependencyProperty ActiveBrushProperty = DependencyProperty.Register(
            "ActiveBrush", typeof(Brush), typeof(AnimationLayer), new PropertyMetadata(new SolidColorBrush(Color.FromArgb(64, 0, 0, 0)),
                (s, e) => { ((WaveToggle)s)._rectActiveBg.Fill = (Brush)e.NewValue; }));


        private readonly Rectangle _rectActiveBg = new Rectangle()
        {
            Opacity = 0,
            Fill = new SolidColorBrush(Colors.Bisque)
        };

        //private readonly Rectangle _rectMouseEnterBg = new Rectangle()
        //{
        //    Opacity = 0,
        //    Fill = new SolidColorBrush(Colors.Bisque)
        //};

        private readonly DoubleAnimation _clickWaveAnim = new DoubleAnimation() { From = 5 };
        private readonly DoubleAnimation _clickWaveOpacityAnim = new DoubleAnimation();
        private readonly PowerEase _easeFunction = new PowerEase() { EasingMode = EasingMode.EaseInOut, Power = 4 };
        private readonly Ellipse _ellipseClickWave;

        public static readonly DependencyProperty ActivatedProperty = DependencyProperty.Register(
            "Activated", typeof(bool), typeof(WaveToggle), new PropertyMetadata(default(bool),
                (s, e) => { ((WaveToggle) s).BeginActivateAnimation(); }
                ));

        public bool Activated
        {
            get => (bool)GetValue(ActivatedProperty);
            set => SetValue(ActivatedProperty, value);
        }


        public WaveToggle()
        {
            Loaded += MaterialCard_Loaded;
            UseLayoutRounding = true;
            _clickWaveAnim.EasingFunction
                = _clickWaveOpacityAnim.EasingFunction
                    = _easeFunction;

            DepthAnim.EasingFunction =
                RadiusAnim.EasingFunction = new PowerEase() { EasingMode = EasingMode.EaseOut, Power = 3 };

            _clickWaveOpacityAnim.SpeedRatio = AnimationSpeed / 2;
            _clickWaveAnim.SpeedRatio = AnimationSpeed;

            DepthAnim.SpeedRatio = RadiusAnim.SpeedRatio = AnimationSpeed * 2;


            //MouseEnter += (s, e) => { BeginMouseEnterAnim(e); };
            //MouseLeave += (s, e) => { BeginMouseLeaveAnim(e); };

            //MouseDown += MaterialCard_MouseDown;


            _ellipseClickWave = new Ellipse
            {
                HorizontalAlignment = HorizontalAlignment.Left,
                VerticalAlignment = VerticalAlignment.Top,
                RenderTransform = new ScaleTransform(),
                RenderTransformOrigin = new Point(0.5, 0.5),
                Height = 0,
                Width = 0,
            };



            CanvasAnimation.Children.Add(_rectActiveBg);
            CanvasAnimation.SizeChanged += (s, e) =>
            {
                 _rectActiveBg.Height = e.NewSize.Height;
                _rectActiveBg.Width = e.NewSize.Width;
            };
            //CanvasAnimation.Children.Add(_rectMouseEnterBg);
            CanvasAnimation.Children.Add(_ellipseClickWave);
            CanvasAnimation.ClipToBounds = true;

        }

        private void MaterialCard_Loaded(object sender, RoutedEventArgs e)
        {
            _rectActiveBg.Fill = ActiveBrush;
            //if (!(ActiveBrush is SolidColorBrush solidColor)) return;
            //Color activeColor = solidColor.Color;
            //ColorHsv activeHsv = activeColor.ToHsv();
            //activeHsv.V = activeHsv.V > 128 ? (byte)(activeHsv.V * 0.8) : (byte)(activeHsv.V * 1.5);
            //activeHsv.A = 128;
            //_rectMouseEnterBg.Fill = new SolidColorBrush(activeHsv.ToRgb().ToMedia());
        }

        //private void BeginMouseEnterAnim(System.Windows.Input.MouseEventArgs e)
        //{
        //    if (!MouseEnterAnimation) return;

        //    var animation = new DoubleAnimation()
        //    {
        //        To = 0.3,
        //        SpeedRatio = AnimationSpeed * 2,
        //        EasingFunction = new PowerEase() { EasingMode = EasingMode.EaseOut, Power = 4 }
        //    };

        //    _rectMouseEnterBg.BeginAnimation(Canvas.OpacityProperty, animation);

        //}

        //private void BeginMouseLeaveAnim(System.Windows.Input.MouseEventArgs e)
        //{
        //    if (!MouseEnterAnimation) return;

        //    var ca = new DoubleAnimation()
        //    {
        //        To = 0,
        //        SpeedRatio = AnimationSpeed * 1.5,
        //        EasingFunction = new PowerEase() { EasingMode = EasingMode.EaseOut, Power = 4 }
        //    };
        //    _rectMouseEnterBg.BeginAnimation(Canvas.OpacityProperty, ca);
        //}


        private void BeginActivateAnimation()
        {
            if (!MouseDownWave) return;


            var bgAnimation = new DoubleAnimation() { SpeedRatio = 2 };
            Point p = Mouse.PrimaryDevice.GetPosition(this);
            Canvas.SetLeft(_ellipseClickWave, p.X);
            Canvas.SetTop(_ellipseClickWave, p.Y);

            double radius = Math.Max(ActualHeight * 1.414, ActualWidth * 1.414) * 2;
            _ellipseClickWave.Width = _ellipseClickWave.Height = 10;

            _clickWaveAnim.To = radius / 10;
            _clickWaveOpacityAnim.From = 1;
            _clickWaveOpacityAnim.To = 0;
            if (Activated) //激活状态
            {
                _ellipseClickWave.Fill = ActiveBrush;
                bgAnimation.To = 1;
            }
            else
            {
                _ellipseClickWave.Fill = CanvasAnimation.Background;
                bgAnimation.To = 0;
            }

            _ellipseClickWave.RenderTransform.BeginAnimation(ScaleTransform.ScaleXProperty, _clickWaveAnim);
            _ellipseClickWave.RenderTransform.BeginAnimation(ScaleTransform.ScaleYProperty, _clickWaveAnim);
            _ellipseClickWave.BeginAnimation(OpacityProperty, _clickWaveOpacityAnim);
            _rectActiveBg.BeginAnimation(Shape.OpacityProperty, bgAnimation);
        }
    }
}